import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Asks for user input and creates a Board
 * @author Matthew Lingelbach
 *
 */

public class TrafficJam {
	private String name = "";
	private int boardX = -1, boardY = -1;
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try{
			new TrafficJam();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public TrafficJam() throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in) );
		
		// Ask for name
		while(name.equals("")){
			System.out.println("Enter your name: ");
			name = br.readLine();
			if(name.isEmpty())
				System.out.println("Invalid name.  Try again.");
		}
		
		//Ask for board size
		while(boardX <= 0 && boardY <= 0){
			System.out.println("Enter the size of the board (ex: 2x2, 5x4): ");
			String board = br.readLine();
			if(!board.contains("x"))
				System.out.println("Please use x to separate the width from gthe height (3x4).");
			String[] boardVals = board.split("x");
			if(boardVals.length == 1){
				System.out.println("Please enter two numbers, separated by an x (3x4).");
				continue;
			}
			else if(boardVals.length > 2)
				System.out.println("This game only supports 2D grids.  The first two numbers will be used.");
			try{
				boardX = Integer.parseInt(boardVals[0]);
				boardY = Integer.parseInt(boardVals[1]);
			}catch(NumberFormatException e){
				System.out.println("Please use numbers.");
				boardX = -1; boardY = -1;
				continue;
			}
		}
		
		build();
	    
	}
	
	private void build(){
		Board board = new Board(boardX, boardY);
		board.setLocationRelativeTo(null);
		board.setVisible(true);
	}

}
